package com.itheima.service.impl;


import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.RoleDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService{

    @Autowired
    private RoleDao roleDao;

    //添加新的角色
    @Override
    public void add(Role role, Integer[] permissionIds) {

        //给角色表添加新的角色
        roleDao.addNewRole(role);

        Integer roleId = role.getId();
        //给角色和权限的关联表添加数据
        this.setNewRoleAndPermission(roleId,permissionIds);

    }

    //分页查询
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();

        PageHelper.startPage(currentPage,pageSize);
        Page<Role> page = roleDao.findByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    //根据角色id查询角色信息
    @Override
    public Role findById(Integer id) {

        return roleDao.findById(id);
    }

    //根据角色id查询权限的id集合
    @Override
    public List<Integer> findPermissionIds(Integer id) {
        return roleDao.findPermissionIds(id);
    }

    //更新角色信息
    @Override
    public void editRole(Role role, Integer[] permissionIds) {

        //根据角色id更新角色信息
        roleDao.updateRoleByRoleId(role);

        Integer roleId = role.getId();
        //根据角色id删除关联关系
        roleDao.deleteRoleIdAndPermissionId(roleId);

        //给角色和权限设置新的关联关系
        this.setNewRoleAndPermission(roleId,permissionIds);
    }

    //第一次删除角色
    @Override
    public Result deleteFirst(Integer id) {
        //根据id查找是否有父表关联 查用户_角色表
        Long count1 = roleDao.findCountFromUserAndRole(id);
        if (count1 > 0){
            return new Result(true, MessageConstant.ASSOCIATED_DATA_DELETE_FAIL,"hasParent");
        }
        //根据id查找是否有子表关联  这里只查角色_权限表
        List<Integer> permissionIds = roleDao.findPermissionIds(id);
        if (permissionIds != null && permissionIds.size() > 0){

            return new Result(true,MessageConstant.ASSOCIATED_DATA_DELETE_FAIL,"hasSon");
        }
        //没有关联 删除角色信息
        roleDao.deleteByRoleId(id);

        return new Result(true,MessageConstant.DELETE_ROLE_SUCCESS,"N");
    }

    //第二次删除角色包括子关联
    @Override
    public void deleteSecond(Integer id) {
        //删除子关联
        roleDao.deleteRoleIdAndPermissionId(id);
        //删除角色信息
        roleDao.deleteByRoleId(id);

    }
    //查询所有的角色
    @Override
    public List<Role> findAll() {
        return roleDao.findAll();
    }


    //给角色和权限设置新的关联关系
    public void setNewRoleAndPermission(Integer roleId,Integer[] permissionIds){

        if (permissionIds != null && permissionIds.length != 0){

            for (Integer permissionId : permissionIds) {

                Map<String,Integer> map = new HashMap<>();
                map.put("roleId",roleId);
                map.put("permissionId",permissionId);
                roleDao.addNewRoleAndPermission(map);
            }
        }
    }
}
